<template>
  <div id="NewsInformation">
    <div class="container">
      <div class="container text-center">
        <h3>服务预约</h3>
        <p style="color: #b2b2b2">Service Reservation</p>
      </div>

      <div class="news-container">
        <div
          class="news-card"
          v-for="item in postList"
          :key="item.id"
          @click="goDetail(item.id)"
        >
          <div class="img-box">
            <img :src="getImg(item.cover)" alt="" />
          </div>
          <div class="text-box">
            <h3 class="title">{{ item.title }}</h3>
            <h4 class="price">价格：￥{{ item.price }}</h4>
            <p class="content">{{ item.content }}</p>
            <p class="date">
              {{ dayjs(item.createdDate).format("YYYY-MM-DD HH:mm:ss") }}
            </p>
            <p>发布人：{{ item.user ? item.user.username : "管理员" }}</p>
          </div>
        </div>
      </div>
    </div>
    <a-modal
      :maskClosable="false"
      title="发帖"
      :width="1000"
      :visible="visible"
      :confirmLoading="confirmLoading"
      @ok="handleOk"
      @cancel="close"
      cancelText="关闭"
      destroyOnClose
    >
      <a-form-model
        ref="postForm"
        :model="form"
        :rules="rules"
        :label-col="labelCol"
        :wrapper-col="wrapperCol"
      >
        <a-form-model-item label="标题" prop="title">
          <a-input placeholder="请输入标题" v-model="form.title" />
        </a-form-model-item>

        <a-form-model-item label="封面" prop="cover">
          <a-upload
            action="http://localhost:4000/api/upload"
            list-type="picture-card"
            :file-list="fileList"
            @change="handleChange"
            :beforeUpload="beforeUpload"
          >
            <div v-if="fileList.length < 1">
              <a-icon type="plus" />
              <div class="ant-upload-text">Upload</div>
            </div>
          </a-upload>
        </a-form-model-item>
        <a-form-model-item label="价格" prop="price">
          <a-input-number placeholder="请输入价格" v-model="form.price" />
        </a-form-model-item>
        <a-form-model-item label="内容" prop="content">
          <a-textarea
            placeholder="请输入内容"
            v-model="form.content"
            :rows="4"
          />
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </div>
</template>
<script>
import { WOW } from "wowjs";
import { getAction, postAction } from "../util/basic";
import dayjs from "dayjs";
import { formatFile, initFile } from "../util/util";
export default {
  name: "NewsInformation",
  data() {
    return {
      nav: "news",
      postList: [],
      url: {
        add: "/service/add",
        news: "/service/list",
      },
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 8 },
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 16 },
      },

      confirmLoading: false,
      form: {
        title: "",
        cover: "",
        content: "",
        price: 0,
        status: true,
      },
      rules: {
        title: [{ required: true, message: "请输入标题" }],
        price: [{ required: true, message: "请输入价格" }],
      },

      fileList: [],
    };
  },
  mounted() {
    var wow = new WOW();
    wow.init();
    this.initPost();
  },
  methods: {
    dayjs,
    initPost() {
      getAction(this.url.news).then((res) => {
        if (res.success) {
          this.postList = res.result;
        }
      });
    },
    getImg(cover) {
      let url = "";
      if (cover) {
        url = JSON.parse(cover)[0].absolute_path;
      }
      return url;
    },
    switchNav(type) {
      this.nav = type;
    },
    goDetail(id) {
      this.$router.push({
        name: `serviceOrderDetail`,
        params: {
          id: id,
        },
      });
    },
    add() {
      if (sessionStorage.getItem("username")) {
        this.visible = true;
      } else {
        this.$router.push({
          name: `login`,
        });
      }
    },

    close() {
      this.confirmLoading = false;
      this.form = {
        title: "",
        cover: "",
        content: "",
        status: true,
        price: 0,
      };
      this.visible = false;
      this.fileList = [];
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.$refs.postForm.validate((valid) => {
        if (valid) {
          if (this.fileList.length < 1) {
            this.$message.warning("请选择一张封面图");
            return;
          }
          that.confirmLoading = true;
          let httpurl = "";

          if (!this.model.id) {
            httpurl += this.url.add;
          } else {
            httpurl += this.url.edit;
          }
          let formData = Object.assign({}, this.form);
          //时间格式化
          formData.userId = sessionStorage.getItem("userId");
          formData.cover = JSON.stringify(formatFile(this.fileList));
          formData.status = formData.status ? 1 : 0;
          console.log(formData);
          postAction(httpurl, formData)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.confirmLoading = false;
                that.initPost();
                that.close();
              } else {
                that.$message.warning(res.message);
                that.confirmLoading = false;
              }
            })
            .finally(() => {
              that.confirmLoading = false;
            })
            .catch((res) => {
              this.$message.error("服务器出错了，请稍后重试");
            });
        } else {
          that.confirmLoading = false;
          return false;
        }
      });
    },
    handleChange({ fileList }) {
      this.fileList = fileList;
    },
    beforeUpload(file) {
      const isJpgOrPng =
        file.type === "image/jpeg" || file.type === "image/png";
      if (!isJpgOrPng) {
        this.$message.error("只能上传jpeg和png图片!");
      }
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error("图片只能小于2M!");
      }
      return isJpgOrPng && isLt2M;
    },
  },
};
</script>
<style scoped>
.nav {
  margin: 20px 0;
}
.nav > a {
  display: inline-block;
  text-decoration: none;
  width: 120px;
  height: 45px;
  line-height: 45px;
  color: #333;
  border: 1px solid #333;
}
.nav > a.active {
  color: #1e73be;
  border-color: #1e73be;
}
.nav > a:hover {
  color: #1e73be;
  border-color: #1e73be;
}
.news-container {
  overflow: hidden;
  margin-bottom: 0;
}
.news-container > li {
  width: 55.6%;
  height: 120px;
  float: left;
  color: #333;
  text-align: right;
  border-left: 1px solid transparent;
  border-right: 1px solid transparent;
}
.news-container > li:hover {
  color: #1e73be;
  border: 1px solid #1e73be;
  cursor: pointer;
}
.news-container > li:nth-of-type(2n) {
  float: right;
  text-align: left;
}
.news-container > li > .content {
  width: 60%;
  float: left;
  padding: 20px 0;
}
.news-container > li > .time {
  width: 20%;
  float: left;
  padding: 10px 0;
}
.news-container > li > .time > p {
  font-size: 30px;
  margin: 5px 0;
}
.news-container > li > .circle {
  width: 20%;
  height: 100%;
  float: left;
  position: relative;
}
.news-container > li > .circle > img {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  width: 20px;
  height: 20px;
}
.news-container > li > .circle > i {
  display: block;
  width: 1px;
  height: 100%;
  background: #707070;
}
.news-container > li:nth-of-type(2n) > .content {
  float: right;
}
.news-container > li:nth-of-type(2n) > .time {
  float: right;
}
.news-container > li:nth-of-type(2n) > .circle {
  float: right;
}
.news-container > li:nth-of-type(1) > .circle > i {
  height: 50%;
  position: absolute;
  top: 50%;
  left: 50%;
}
.more {
  font-size: 25px;
  color: #707070;
}
.more > i {
  cursor: pointer;
}
.container {
  position: relative;
}
.add {
  position: absolute;
  right: 20px;
  top: 40px;
  padding: 10px;
  color: #fff;
  background: #f65;
  border-radius: 10px;
  cursor: pointer;
  z-index: 2;
}
.news-card {
  padding: 20px;
  border: 1px solid;
  display: flex;
  align-content: center;
  align-self: center;
  margin-bottom: 30px;
  color: rgb(102, 102, 102);
  border-color: rgb(204, 204, 204);
}
.news-card:hover {
  border: 1px solid #1e73be;
  cursor: pointer;
}
.news-card:hover h3 {
  color: #1e73be;
}
.news-card .img-box {
  margin-right: 50px;
}
.news-card .img-box img {
  width: 200px;
  height: 160px;
}
.news-card .content {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}
@media screen and (max-width: 767px) {
  .news-container > li {
    width: 100%;
  }
  .news-container > li > .content {
    width: 70%;
    text-align: left;
    float: right;
  }
  .news-container > li > .time {
    width: 30%;
    text-align: left;
    float: right;
  }
  .news-container > li > .circle {
    display: none;
  }
}
.price{
    color:#f65
}
</style>

